import LXCOM from '../../common/common.js'
import baseunitgrah from '../baseunitgrah.js'
import { CoordTRFUtil } from '../../common/tools/coordtool.js'
import Coord from '../../common/struct/coord.js'
import UtilFuncs from '../../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../../common/globalConst.js'
var global = GlobalConst.GlobalConst;

import graphfactory from '../graphfactory.js'
var factory = graphfactory.GraphFactory;

import { LGXEnums } from '../../common/sysenum.js'
import { GlobalVar } from '../../common/globalvar.js'
import coord from '../../common/struct/coord.js'


import { GraphFourDirFeaturePtUtil } from '../datautil/graphfourdirfeatureptutil.js'

const scale = 1.0;
const frameColor = "rgb(128,128,128)"
const arrowColor = "rgb(153,217,234)";
const arrowSize = 30;
const arrowAngle = 30;
const arrowCntGap = arrowSize * Math.sin(funcs.degree2Radian(arrowAngle * 2)) / 2;
const toolPanelHeight = 40;
const toolPanelWidth = 320;
const toolIconHgt = 16; //图标尺寸
const toolIconSpan = 20;//每个图标占据尺寸
const maxIconCol = toolPanelWidth / toolIconSpan;

class FastToolPanel extends baseunitgrah.BaseUnitGraph {
    constructor(panelSvc) {
        super(0);
        this.panelSvc = panelSvc;
        this.m_graphType = LGXEnums.LGXGraphType.FastToolPanel_type;
        this.leftArrowCnt = { x: 0, y: 0 };
        this.bottomArrowCnt = { x: 0, y: 0 };
        this.rightArrowCnt = { x: 0, y: 0 };
        this.topArrowCnt = { x: 0, y: 0 };
        this.toolPanelDisDesc = "none"; //描述当前显示快速绘图工具栏的位置（left,bottom,right,top）
        this.toolGraphList = []; //显示在快速工具栏上的图形队列，压缩显示
        this.toolGraphPtList = [];//显示在快速工具栏上的图形中心队列，用于快速判断选中
        this.toolTypeList = [];//快速工具栏上显示的工具队列，从数据管理类中获取

        this.setPos(new Coord.PointPos());
        this.width = 100;
        this.height = 100;

        //快速工具烂显示时，其信息描述，包括占据区域、所在位置（跟this.toolPanelDisDesc保持一致）
        this.toolPanelInfo = { rect: new LXCOM.LGXRect(), desc: "none" };
        this.fastCreateGraph = null;//当前快速生成的图形，用于临时显示，鼠标左键按下后，通知应用层正式生成，本图形对象释放
        this.selectToolIndex = -1;//当前选中的工具栏上工具索引
        this.fastCreateGraphPoint = { x: 0, y: 0 };//对应fastCreateGraph的中心，用于返回给应用层生成正式图形时使用
        this.curentTool = {type:0,symbolUUID:''};//由用户选中的，当前绘制的工具，此工具排在工具栏的正中心，并且优先显示

        //描述快捷箭头是否可以显示：0--该侧对面有图形无需显示，1--该侧无图形，可以显示快捷面板
        this.arrowDisFlag = { 'left': 1, 'right': 1, 'top': 1, 'bottom': 1 };
    }

    getToolPanelInfo(){return this.toolPanelInfo;}

    getSelFastToolInfo(mousePoint) {
        let type = -1;
        let point = null;
        let desc = "";
        let symbolUUID = '';

        if (this.selectToolIndex >= 0) {
            //选中快捷工具栏上的其他工具图标
            let dest = this.toolTypeList[this.selectToolIndex];
            type = dest.type;
            symbolUUID = dest.uuid;
            point = this.fastCreateGraphPoint;
            this.selectToolIndex = -1;
            this.toolPanelDisDesc = "none";
        }
        else {
            //本次鼠标摁下之前没选中工具，则判断是否鼠标按下箭头处，如是则可能需要自动生成连接线
            let ret = this.chkMouseOnToolPanelArrow(mousePoint);
            if (ret.flag) {
                type = global.TwoPtline_Topo;
                desc = ret.decs;
                console.log("toolPanelDisDesc:" + this.toolPanelDisDesc);
            }
        }

        return { point, type, desc,symbolUUID };
    }

    setCurentTool(toolType,symbolUUID) {
        let mainType = toolType >> 16;
        if (mainType == global.ConnectLine || mainType == global.BaseUnitPolyline) {
            return;
        }
        this.curentTool.type = toolType;
        this.curentTool.symbolUUID = symbolUUID;
        let n = 0;
        if (!symbolUUID){
            console.log(symbolUUID);
            n = 100;
            console.log(n++);
        }
        console.log(n++);
        let inList = false;
        this.toolTypeList.forEach(t => {
            if (t.type == toolType && t.uuid == symbolUUID) {
                inList = true;
                return;
            }
        });
        if (!inList) {
            let pNewGraph = this.createTempGraph(toolType,symbolUUID);
            if (pNewGraph) {
                this.toolGraphList.push(pNewGraph);
                this.toolGraphPtList.push({ x: 0, y: 0 });
                this.toolTypeList.push({ type: toolType, name: "unkown",uuid:symbolUUID });
            }
        }
    }

    setObserver(dataMng) {
        this.addObserver(dataMng);
    }

    resetRefGraph(refGraph) {
        if (this.refGraph == refGraph) {
            return;
        }
        let refZone = refGraph.getZone();
        var pos = this.getPos();
        pos.x = refZone.getCnt().x;
        pos.y = refZone.getCnt().y;
        this.width = refZone.width() * scale;
        this.height = refZone.height() * scale;
        this.refGraph = refGraph;
        this.initToolIcons();

        this.toolPanelDisDesc = "none";
        this.toolPanelInfo = { rect: new LXCOM.LGXRect(), desc: "none" };
        this.fastCreateGraph = null;
        this.selectToolIndex = -1;
        this.fastCreateGraphPoint = { x: 0, y: 0 };
        this.arrowDisFlag = { 'left': 1, 'right': 1, 'top': 1, 'bottom': 1 };
        this.reSetArrowDisFlag();
    }

    resetPos() {
        let refZone = this.refGraph.getZone();
        var pos = this.getPos();
        pos.x = refZone.getCnt().x;
        pos.y = refZone.getCnt().y;
        this.width = refZone.width() * scale;
        this.height = refZone.height() * scale;
    }

    resetDisInfo() {
        this.toolPanelDisDesc = "none";
        this.toolPanelInfo = { rect: new LXCOM.LGXRect(), desc: "none" };
        this.fastCreateGraph = null;
        this.selectToolIndex = -1;
        this.fastCreateGraphPoint = { x: 0, y: 0 };
        this.reSetArrowDisFlag();
    }

    display(map, mapInfo) {
        console.log("this.toolPanelDisDesc:" + this.toolPanelDisDesc);
        this.disPanelSelArrow(map, mapInfo);//显示选择箭头
        this.diplayRefGraphZone(map, mapInfo);//显示图形矩形包围盒
        this.displayFastNewGraph(map, mapInfo);//显示临时生成的快捷目标图形
        let disFlag = this.disToolPanel(map, mapInfo);//显示快速工具栏面板
        if (disFlag) {
            this.disSelTooFrame(map, mapInfo);//显示快捷工具栏上，鼠标移动时选中的工具图标外框，标识选中
        }
        // this.disArrowCenter(map,mapInfo);//test code

    }

    displayFastNewGraph(map, mapInfo) {
        if (!this.fastCreateGraph) {
            return;
        }
        let autoDir = new LXCOM.LGXVector();
        switch (this.toolPanelDisDesc) {
            case "left":
                autoDir.setValue(-1, 0, 0);
                break;

            case "right":
                autoDir.setValue(1, 0, 0);
                break;

            case "top":
                autoDir.setValue(0, 1, 0);
                break;

            case "bottom":
                autoDir.setValue(0, -1, 0);
                break;

            default:
                return;
        }

        let destGraph = this.fastCreateGraph;
        let pt = this.panelSvc.notifyToCalFastGraphCNT(this.getPos(), this.refGraph, destGraph, autoDir);

        this.fastCreateGraphPoint.x = pt.x;
        this.fastCreateGraphPoint.y = pt.y;

        destGraph.translate({ x: pt.x, y: pt.y }, 1, false);
        destGraph.display(map, mapInfo);
        destGraph.translate({ x: -pt.x, y: -pt.y }, 1, false);
    }


    disToolPanel(map, mapInfo) {
        let disflag = true;
        let panelCnt = { x: 0, y: 0 };
        let wid = toolPanelWidth;
        let hgt = toolPanelHeight;
        let hornVertKind = 1; //0 --水平 1---垂直
        switch (this.toolPanelDisDesc) {
            case "left":
                panelCnt.x = this.leftArrowCnt.x - toolPanelHeight;
                panelCnt.y = this.leftArrowCnt.y;
                wid = toolPanelHeight;
                hgt = toolPanelWidth;
                break;

            case "right":
                panelCnt.x = this.rightArrowCnt.x + toolPanelHeight;
                panelCnt.y = this.rightArrowCnt.y;
                wid = toolPanelHeight;
                hgt = toolPanelWidth;
                break;

            case "top":
                panelCnt.x = this.topArrowCnt.x;
                panelCnt.y = this.topArrowCnt.y + toolPanelHeight;
                hornVertKind = 0;
                break;

            case "bottom":
                panelCnt.x = this.bottomArrowCnt.x;
                panelCnt.y = this.bottomArrowCnt.y - toolPanelHeight;
                hornVertKind = 0;
                break;

            default:
                disflag = false;
        }

        if (!disflag) {
            return false;
        }

        map.save();
        map.lineWidth = 1;
        map.strokeStyle = frameColor;
        let scaling = mapInfo.getScaling();
        let diswidth = wid * scaling;
        let disheight = hgt * scaling;
        let ltPt = { x: 0, y: 0 };
        ltPt.x = panelCnt.x - wid / 2;
        ltPt.y = panelCnt.y + hgt / 2;
        var ltScreenPt = CoordTRFUtil.world2Screen(ltPt, mapInfo);
        map.beginPath();
        map.rect(ltScreenPt.x, ltScreenPt.y, diswidth, disheight);
        map.closePath();
        map.stroke();
        map.fillStyle = GlobalVar.backcolor.toHexString();
        map.fill();
        map.restore();

        this.toolPanelInfo.desc = this.toolPanelDisDesc;
        this.toolPanelInfo.rect.left = ltPt.x;
        this.toolPanelInfo.rect.top = ltPt.y;
        this.toolPanelInfo.rect.right = ltPt.x + wid;
        this.toolPanelInfo.rect.bottom = ltPt.y - hgt;

        this.disToolistOnPanel(panelCnt, hornVertKind, map, mapInfo);
        return disflag;
    }

    diplayRefGraphZone(map, mapInfo) {
        map.save();
        map.lineWidth = 1;
        map.strokeStyle = frameColor;
        let scaling = mapInfo.getScaling();
        let refZone = this.refGraph.getZone();
        let diswidth = refZone.width() * scaling;
        let disheight = refZone.height() * scaling;
        let ltPt = refZone.ltPt();
        var ltScreenPt = CoordTRFUtil.world2Screen(ltPt, mapInfo);
        map.rect(ltScreenPt.x, ltScreenPt.y, diswidth, disheight);
        map.stroke();
        map.restore();
    }

    disPanelSelArrow(map, mapInfo) {
        var postion = this.getPos();
        let cntPt = { x: postion.x, y: postion.y };
        map.save();

        let x = 0;
        let y = 0;

        if (this.arrowDisFlag.top) {
            x = cntPt.x;
            y = cntPt.y + this.height / 2;
            let topMidPt = { x, y };
            let topMidDir = new LXCOM.LGXVector();
            topMidDir.setValue(0, 1, 0);
            this.disSelArrow(topMidPt, topMidDir, map, mapInfo);
            this.topArrowCnt.x = x;
            this.topArrowCnt.y = y + arrowCntGap;
        }

        if (this.arrowDisFlag.left) {
            x = cntPt.x - this.width / 2;
            y = cntPt.y;
            let leftMidPt = { x, y };
            let leftMidDir = new LXCOM.LGXVector();
            leftMidDir.setValue(-1, 0, 0);
            this.disSelArrow(leftMidPt, leftMidDir, map, mapInfo);
            this.leftArrowCnt.x = x - arrowCntGap;
            this.leftArrowCnt.y = y;
        }

        if (this.arrowDisFlag.bottom) {
            x = cntPt.x;
            y = cntPt.y - this.height / 2;
            let bottomMidPt = { x, y };
            let bottomMidDir = new LXCOM.LGXVector();
            bottomMidDir.setValue(0, -1, 0);
            this.disSelArrow(bottomMidPt, bottomMidDir, map, mapInfo);
            this.bottomArrowCnt.x = x;
            this.bottomArrowCnt.y = y - arrowCntGap;
        }

        if (this.arrowDisFlag.right) {
            x = cntPt.x + this.width / 2;
            y = cntPt.y;
            let rightMidPt = { x, y };
            let rightMidDir = new LXCOM.LGXVector();
            rightMidDir.setValue(1, 0, 0);
            this.disSelArrow(rightMidPt, rightMidDir, map, mapInfo);
            this.rightArrowCnt.x = x + arrowCntGap;
            this.rightArrowCnt.y = y;
        }
        map.restore();
    }

    disSelArrow(arrowPt, arrowDir, map, mapInfo) {
        let lnvct = arrowDir.rotateCopy(180);

        let scaling = mapInfo.getScaling();
        var gap = arrowSize / scaling;

        let arrowHgt = arrowSize * Math.cos(funcs.degree2Radian(arrowAngle));
        let arroeVertexPt = { x: 0, y: 0 };
        arroeVertexPt.x = arrowPt.x + arrowDir.m * arrowHgt;
        arroeVertexPt.y = arrowPt.y + arrowDir.n * arrowHgt;
        var arrowSPt = CoordTRFUtil.world2Screen(arroeVertexPt, mapInfo);

        var rotLeftVct = lnvct.cloneCopy();
        rotLeftVct.rotate(arrowAngle);
        var x = arroeVertexPt.x + gap * rotLeftVct.m;
        var y = arroeVertexPt.y + gap * rotLeftVct.n;
        var pt = { x, y };
        var leftPt = CoordTRFUtil.world2Screen(pt, mapInfo);

        var rotRightVct = lnvct.cloneCopy();
        rotRightVct.rotate(-arrowAngle);
        x = arroeVertexPt.x + gap * rotRightVct.m;
        y = arroeVertexPt.y + gap * rotRightVct.n;
        pt = { x, y };
        var rightPt = CoordTRFUtil.world2Screen(pt, mapInfo);

        map.beginPath();
        map.lineWidth = 1;
        map.strokeStyle = arrowColor;
        map.moveTo(arrowSPt.x, arrowSPt.y);
        map.lineTo(leftPt.x, leftPt.y);
        map.lineTo(rightPt.x, rightPt.y);
        map.closePath();
        map.stroke();
        map.fillStyle = arrowColor;
        map.fill();
    }

    //根据当前鼠标位置检测是否需要显示工具栏面板
    chkNeedDisToolPanel(mousePoint) {
        let preDesc = this.toolPanelDisDesc;
        let ret = this.chkMouseOnToolPanelArrow(mousePoint);
        if (preDesc != 'none' && ret.decs == 'none'){
            return false;
        }
        else{
            this.toolPanelDisDesc = ret.decs;
        }
        let flag = ret.flag;
        return flag;
    }

    //检测鼠标是否在快捷标识箭头上
    chkMouseOnToolPanelArrow(mousePoint) {
        let arrowDecs = "none";
        let flag = false;
        if (this.arrowDisFlag.left && this.chkInArrow(mousePoint, this.leftArrowCnt)) {
            arrowDecs = "left";
            flag = true;
        }
        else if (this.arrowDisFlag.top && this.chkInArrow(mousePoint, this.topArrowCnt)) {
            arrowDecs = "top";
            flag = true;
        }
        else if (this.arrowDisFlag.right && this.chkInArrow(mousePoint, this.rightArrowCnt)) {
            arrowDecs = "right";
            flag = true;
        }
        else if (this.arrowDisFlag.bottom && this.chkInArrow(mousePoint, this.bottomArrowCnt)) {
            arrowDecs = "bottom";
            flag = true;
        }
        return { flag, decs: arrowDecs };
    }

    chkInArrow(mousePoint, destPt) {
        let dis = funcs.distancePoint(mousePoint, destPt, 1);
        if (dis >= 0 && dis < arrowSize / 2) {
            return true;
        }
        else {
            return false;
        }
    }

    checkPtInScope(mousePoint, scaling) {
        let flag1 = this.preCheckInScope(mousePoint, 1);
        let flag2 = this.chkNeedDisToolPanel(mousePoint);  //根据当前鼠标位置检测是否需要显示工具栏面板
        let flag3 = this.checkInToolPanelScope(mousePoint, scaling);//检测当前鼠标位置在快速工具栏的哪个快速图标上
        let flag4 = this.chechInArrowScope(mousePoint, scaling);
        return (flag1 || flag2 || flag3 || flag4);
    }

    checkCanFastCreat(mousePoint, scaling) {
        if (!this.checkPtInScope(mousePoint,scaling)){
            return false;
        }
        let flag2 = this.chkNeedDisToolPanel(mousePoint);  //根据当前鼠标位置检测是否需要显示工具栏面板
        let flag3 = this.checkInToolPanelScope(mousePoint, scaling);//检测当前鼠标位置在快速工具栏的哪个快速图标上
        return (flag2||flag3);
    }

    //检测鼠标是否在四个快捷箭头标识围成的一个矩形范围内
    chechInArrowScope(mousePoint, scaling) {
        let tempMulPos = new coord.MultiPos();
        tempMulPos.addPoint(this.leftArrowCnt);
        tempMulPos.addPoint(this.bottomArrowCnt);
        tempMulPos.addPoint(this.rightArrowCnt);
        tempMulPos.addPoint(this.topArrowCnt);
        let rect = tempMulPos.getZone();
        rect.expand(global.SelectGap / scaling);
        let flag = funcs.chkPtInRect(mousePoint, rect);
        return flag;
    }

    //检测是否在快捷箭头上悬停，如果悬停了，则临时自动生成一个对应当前优先工具生成的图形，放在箭头的一侧
    chkHoverOnArrow(mousePoint) {
        let ret = this.chkMouseOnToolPanelArrow(mousePoint);
        let flag = ret.flag;
        if (flag) {
            for (let i = 0; i < this.toolTypeList.length; i++) {
                let t = this.toolTypeList[i];
                if (t.type == this.curentTool.type
                    && t.uuid == this.curentTool.symbolUUID
                ) {
                    this.selectToolIndex = i;
                    break;
                }
            }

            if (this.selectToolIndex != -1) {
                let preGraph = this.fastCreateGraph;
                let needNewFlag = false;
                if (!preGraph) {
                    needNewFlag = true
                }
                else{
                    if (preGraph.getType() == this.curentTool.type 
                    && preGraph.refSymbolUUID == this.curentTool.symbolUUID){
                        needNewFlag = false;
                    }
                    else{
                        needNewFlag = true;
                    }
                }
                if (needNewFlag) {
                    let pNewGraph = this.createTempGraph(this.curentTool.type,this.curentTool.symbolUUID);
                    if (pNewGraph) {
                        this.fastCreateGraph = pNewGraph;
                    }
                }
            }
            else {
                this.fastCreateGraph = null;
                this.selectToolIndex = -1;
            }
        }
    }

    checkInToolPanelScope(mousePoint, scaling) {
        if (this.toolPanelInfo.desc == "none") {
            this.fastCreateGraph = null;
            this.selectToolIndex = -1;
            return false;
        }
        let rect = new LXCOM.LGXRect();
        rect.clone(this.toolPanelInfo.rect);
        rect.expand(global.SelectGap / scaling);
        let flag = funcs.chkPtInRect(mousePoint, rect);
        if (flag) {
            this.toolPanelDisDesc = this.toolPanelInfo.desc;
            let selDstGraph = null;
            for (let i = 0; i < this.toolGraphPtList.length; i++) {
                let pt = this.toolGraphPtList[i];
                if (this.chkInArrow(mousePoint, pt)) {
                    selDstGraph = this.toolGraphList[i];
                    this.selectToolIndex = i;
                    break;
                }
            }

            if (selDstGraph) {
                let preGraph = this.fastCreateGraph;
                let needNewFlag = false;
                if (!preGraph) {
                    needNewFlag = true
                }
                else{
                    if (preGraph.getType() == selDstGraph.getType()
                    && preGraph.refSymbolUUID == selDstGraph.refSymbolUUID){
                        needNewFlag = false;
                    }
                    else{
                        needNewFlag = true;
                    }
                }

                let refSymbolUUID = '';
                if (selDstGraph.getSymbol()){
                    refSymbolUUID = selDstGraph.getSymbol().uuid;
                }

                if (needNewFlag) {
                    let pNewGraph = this.createTempGraph(selDstGraph.getType(),refSymbolUUID);
                    if (pNewGraph) {
                        this.fastCreateGraph = pNewGraph;
                    }
                }
            }
            else {
                let ret = this.chkMouseOnToolPanelArrow(mousePoint);
                if (!ret.flag) {
                    this.fastCreateGraph = null;
                    this.selectToolIndex = -1;
                }
            }
        }
        return flag;
    }

    distance(tPoint, scaling, mapscale) {
        return -1;
    }

    calculateSubKind(tPoint, scaling, mapscale) {
        var nRefKind = 0;
        return nRefKind;
    }

    calScope(mapscale) {
        var postion = this.getPos();
        this._zone.left = (postion.x - this.width / 2);
        this._zone.bottom = (postion.y - this.height / 2);
        this._zone.right = (postion.x + this.width / 2);
        this._zone.top = (postion.y + this.height / 2);
    }

    getLocalZone() {
        let rect = new LXCOM.LGXRect(0, 0, 0, 0);
        rect.left = (- this.width / 2);
        rect.bottom = (- this.height / 2);
        rect.right = (this.width / 2);
        rect.top = (this.height / 2);
        return rect;
    }

    disArrowCenter(map, mapInfo) {
        let ptlist = [];
        ptlist.push(this.leftArrowCnt);
        ptlist.push(this.rightArrowCnt);
        ptlist.push(this.topArrowCnt);
        ptlist.push(this.bottomArrowCnt);

        for (let i = 0; i < ptlist.length; i++) {
            var pt = ptlist[i];
            var p = CoordTRFUtil.world2Screen(pt, mapInfo);
            this.drawFocusPoint(map, p.x, p.y, "#0000ff");
        }
    }

    initToolIcons() {
        let count = this.dataChgObserverList.length;
        if (count != 1) {
            return;
        }

        this.toolGraphList = [];
        this.toolGraphPtList = [];
        this.toolTypeList = [];

        let observer = this.dataChgObserverList[0];
        let toolList = observer.notify2GetTool(this.refGraph.getType());
        let toolcount = toolList.length;
        for (let i = 0; i < toolcount; i++) {
            let tool = toolList[i];
            var pNewGraph = this.createTempGraph(tool.type,tool.uuid);
            if (pNewGraph) {
                this.toolGraphList.push(pNewGraph);
                this.toolGraphPtList.push({ x: 0, y: 0 });
                this.toolTypeList.push(tool);
            }
            else{
                console.log('error type');
            }
        }
        if (this.curentTool != 0) {
            this.setCurentTool(this.curentTool.type,this.curentTool.symbolUUID);
        }
        console.log(this.toolTypeList);
    }

    createTempGraph(type,symbolUUID) {
        if (this.dataChgObserverList.length != 1) {
            return null;
        }

        let observer = this.dataChgObserverList[0];
        var pNewGraph = factory.createGraphByType32(type, null);
        if (!pNewGraph){
            return null;
        }
        if (pNewGraph.m_graphType == LGXEnums.LGXGraphType.MoldGraph_type) {
            let symbol = observer.getSymbol(symbolUUID, 0);
            if (symbol) {
                pNewGraph.setSymbol(symbol);
            }
            else {
                pNewGraph = null;
            }
        }

        if (pNewGraph && (pNewGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
            || pNewGraph.m_graphType == LGXEnums.LGXGraphType.MultiSel_Rect_Type
            || pNewGraph.m_graphType == LGXEnums.LGXGraphType.FastToolPanel_type
            || pNewGraph.m_graphType == LGXEnums.LGXGraphType.CommBusGraph_type
            || pNewGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type
            || pNewGraph.m_graphType == LGXEnums.LGXGraphType.SplineGraph_type)) {
            return null;
        }

        if (pNewGraph) {
            pNewGraph.setUseful(1);
            pNewGraph.setStart(1);
            pNewGraph.setFinish(1);
            pNewGraph.calScope(1);
        }
        return pNewGraph;
    }

    disToolistOnPanel(panelCnt, hornVertKind, map, mapInfo) {
        console.log("disToolistOnPanel__________________________");
        let toolcount = this.toolGraphList.length;
        let maxCol = maxIconCol;
        let midColIdx = Math.floor(maxCol / 2);
        let rowCount = toolcount / maxIconCol;
        let midRowIdx = Math.ceil(rowCount / 2);
        for (let i = 0; i < this.toolGraphList.length; i++) {
            let unitGraph = this.toolGraphList[i];
            let rect = unitGraph.getZone();
            let scalex = toolIconHgt / rect.width();
            let scaley = toolIconHgt / rect.height();
            let rowidx = Math.floor(i / maxCol);
            let colIdx = i % maxCol;
            let x = panelCnt.x + (colIdx - midColIdx + 0.5) * toolIconSpan;
            let y = panelCnt.y + (midRowIdx - rowidx - 0.5) * toolIconSpan;
            if (hornVertKind == 1) {
                y = panelCnt.y - (colIdx - midColIdx + 0.5) * toolIconSpan;
                x = panelCnt.x + (midRowIdx - rowidx - 0.5) * toolIconSpan;
            }
            let toolIconCntPt = this.toolGraphPtList[i];
            toolIconCntPt.x = x;
            toolIconCntPt.y = y;
            var cntScreenPT = CoordTRFUtil.world2Screen(toolIconCntPt, mapInfo);

            map.save();
            map.translate(cntScreenPT.x, cntScreenPT.y);
            map.scale(scalex, scaley);
            map.translate(-cntScreenPT.x, -cntScreenPT.y);
            unitGraph.translate({ x, y }, 1, false);
            unitGraph.display(map, mapInfo);
            unitGraph.translate({ x: -x, y: -y }, 1, false);
            map.restore();
        }
    }

    disSelTooFrame(map, mapInfo) {
        if (this.selectToolIndex >= 0 && this.selectToolIndex < this.toolGraphPtList.length) {
            let toolIconCntPt = this.toolGraphPtList[this.selectToolIndex];
            let left = toolIconCntPt.x - toolIconHgt / 2;
            let top = toolIconCntPt.y + toolIconHgt / 2;
            let spt = CoordTRFUtil.world2Screen({ x: left, y: top }, mapInfo);
            map.lineWidth = 2;
            map.strokeStyle = arrowColor;
            map.beginPath();
            map.rect(spt.x, spt.y, toolIconHgt, toolIconHgt);
            map.closePath();
            map.stroke();
        }
    }

    setUseful(v) {
        this._useful = v;
        if (v == 1) {
            this.reSetArrowDisFlag();
        }
    }

    reSetArrowDisFlag() {
        if (!this.refGraph) {
            return;
        }
        this.arrowDisFlag = { 'left': 0, 'right': 0, 'top': 0, 'bottom': 0 };

        let rightPt = GraphFourDirFeaturePtUtil.getEastFeaturePt(this.refGraph);
        if (!this.chkPtConnNode(rightPt)) {
            this.arrowDisFlag.right = 1;
        }
        let leftPt = GraphFourDirFeaturePtUtil.getWestFeaturePt(this.refGraph);
        if (!this.chkPtConnNode(leftPt)) {
            this.arrowDisFlag.left = 1;
        }
        let topPt = GraphFourDirFeaturePtUtil.getNorthFeaturePt(this.refGraph);
        if (!this.chkPtConnNode(topPt)) {
            this.arrowDisFlag.top = 1;
        }
        let bottomPt = GraphFourDirFeaturePtUtil.getSouthFeaturePt(this.refGraph);
        if (!this.chkPtConnNode(bottomPt)) {
            this.arrowDisFlag.bottom = 1;
        }

    }

    chkPtConnNode(pt) {
        if (!this.refGraph.m_topoRes) {
            return false;
        }

        let flag = false;
        const cmpGap = global.SelectGap / 4;
        let refGraphConnNodeLst = this.refGraph.m_topoRes.getConnNodeList();
        refGraphConnNodeLst.forEach(node => {
            if (node.getUseful() == 1) {
                let nodePt = node.getPos();
                let dis = funcs.distancePoint(pt, nodePt, 1);
                if (dis < cmpGap) {
                    let connLnLst = node.getConnValidLineList();
                    if (connLnLst && connLnLst.length > 0) {
                        flag = true;
                        return;
                    }
                }
            }
        });
        return flag;
    }
}

export default { FastToolPanel }
